package com.das.mechanic_base.adapter.camera;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.camera.Folder;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;

public class X3FolderAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<Folder> mFolders = new ArrayList();
    private final RequestOptions options;

    public X3FolderAdapter(Context context){
        mContext = context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        options = new RequestOptions()
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .transforms(new CenterCrop(),new RoundedCorners(4))
                .error(new ColorDrawable(mContext.getResources().getColor(R.color.user_text_color_unselect)));
    }

    /**
     * 设置数据集
     * @param folders
     */
    public void setData(List<Folder> folders) {
        if(!X3StringUtils.isListEmpty(folders)){
            mFolders = folders;
        }else{
            mFolders.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mFolders.size() + 1;
    }

    @Override
    public Folder getItem(int i) {
        if(i == 0) {
            return null;
        }
        return mFolders.get(i-1);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if(view == null){
            view = mInflater.inflate(R.layout.x3_folder_item, viewGroup, false);
            holder = new ViewHolder(view);
        }else{
            holder = (ViewHolder) view.getTag();
        }

        if(i == 0){
            if(mFolders.size() > 0){
                holder.tv_title.setText(mContext.getString(R.string.x3_near_photo));
                holder.tv_num.setText(getTotalImageSize() + "");
                holder.bindData(mFolders.get(0));
            }
        }else {
            Folder f = getItem(i);
            holder.tv_title.setText(f.name);
            holder.tv_num.setText(f.images.size() + "");
            holder.bindData(getItem(i));
        }

        return view;
    }

    private int getTotalImageSize(){
        int result = 0;
        if(mFolders != null && mFolders.size()>0){
            for (Folder f: mFolders){
                result += f.images.size();
            }
        }
        return result;
    }

    class ViewHolder{
        ImageView iv_img;
        TextView tv_title;
        TextView tv_num;

        ViewHolder(View view){
            iv_img = view.findViewById(R.id.iv_img);
            tv_title = view.findViewById(R.id.tv_title);
            tv_num = view.findViewById(R.id.tv_num);
            view.setTag(this);
        }

        void bindData(Folder data) {
            // 显示图片
            if(mFolders.size() > 0) {
                if (mContext == null) {
                    return;
                }
                Glide.with(mContext)
                        .load(data.cover.path)
                        .thumbnail(0.5f)
                        .apply(options)
                        .into(iv_img);
            }
        }
    }
}
